/**
 * 项目详情设置
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    require('authorize');
    require("select");
    require("permission");
    require('global');
    require("/static_public/js/lib/umeditor.cmd");

    var storage = require("storage");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/building-info/"; //api前缀

    var BuildingSetting = {
        proj_id:"", //选中的项目
        building_id:"",
        building_data:null, //楼盘数据
        init: function() {
            //获取项目列表
            this.bindEvent();
            util.getProjList(function(data){
                this.initProjList(data);
            },this);
            this.changeUrl();
        },
        initProjList:function(list){
            var data = [];
            $.each(list,function(i,item){
                data.push({
                    text:item.ProjName,
                    value:item.p_projectId
                });
            });
            //初始化项目下拉框
            var cfg = {
                id:"proj_container",
                autoSelect:true,
                list:data,
                context:this,
                enableSearch:true,
                callback:function(proj){
                    this.changeProj(proj);
                }
            };
            //如果url有传递proj_id，则选中指定的项目
            var proj_id = util.getUrlPara("proj_id");
            if(proj_id){
                cfg.defValue = proj_id;
            }else{
                //从本地存储中读取
                var obj = storage.get({key:"angular.ui.projectId"});
                if(obj.value){
                    cfg.defValue = obj.value;
                }
            }
            $.select(cfg);
        },
        changeProj:function(proj){
            this.proj_id = proj.value; //选中的项目
            this.getBuildingInfo(true);
            storage.set({key:"angular.ui.projectId",value:this.proj_id});
        },
        //切换项目时获取项目楼盘id
        getBuildingInfo:function(refresh){
            var url = api_prefix + "get-building-info";
            var data = {
                proj_id:this.proj_id
            };
            util.myAjax({
                url: url,
                type: "get",
                data:data,
                context: this,
                success: function(json) {
                    if (json.retCode == 0) {
                        this.building_data = json.data;
                        this.building_id = this.building_data ? this.building_data.b_regbroker_building_infoId : "";
                        if(refresh){
                            this.changeUrl();
                        }
                    } else {
                        ui.showErrTip(json.errMsg);
                    }
                }
            });
        },
        bindEvent: function() {
            var _this = this;
            $("#tab_list li").on("click",function(e){
                if(typeof _this.checkDataModify == "function"){
                    //楼盘详情页面数据修改后，切换tab页时给出提示
                    var isModify = _this.checkDataModify();
                    if(isModify){
                        util.confirm("页面已做修改，确认离开此页面吗？",{ok:function(){
                            $(e.currentTarget).addClass("tab-sm-on").siblings().removeClass("tab-sm-on");
                            _this.changeUrl();
                        }});
                        return;
                    }
                }
                $(this).addClass("tab-sm-on").siblings().removeClass("tab-sm-on");
                _this.changeUrl();
            });
        },
        changeUrl:function(){
            var url = $("#tab_list li.tab-sm-on").data("url");
            if(url){
                this.curUrl = url;
                $("#content_wrap").load(url + "?_t=" + Math.random());
            }
        }
    };
    module.exports = BuildingSetting;
});